<script lang="ts" setup>
import { reactive, ref } from "vue";

import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

import { authApi } from "@/api/modules/auth.api";

const router = useRouter();

// 表单数据
const loginType = ref<"username" | "phone">("username");
const form = reactive({
  username: "",
  phone: "",
  password: "",
});

// 表单验证规则
const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  phone: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "请输入正确的手机号",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, message: "密码长度不能少于6位", trigger: "blur" },
  ],
};

const formRef = ref();
const loading = ref(false);

// 提交登录
const submitForm = async () => {
  try {
    // 验证表单
    await formRef.value.validate();

    loading.value = true;

    // 调用登录API
    const loginData =
      loginType.value === "username"
        ? { username: form.username, password: form.password }
        : { phone: form.phone, password: form.password };

    const response = await authApi.login(loginData);

    // 保存token到localStorage
    localStorage.setItem("token", response.data.token);
    localStorage.setItem("user", JSON.stringify(response.data.user));

    ElMessage.success("登录成功");

    // 跳转到首页
    router.push("/");
  } catch (error: any) {
    console.error("登录失败:", error);
    if (error.response?.data?.message) {
      ElMessage.error(error.response.data.message);
    } else {
      ElMessage.error("登录失败，请重试");
    }
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <div
    class="login-page w-full h-screen flex items-center justify-center bg-gray-100"
  >
    <div class="login-container bg-white p-8 rounded-lg shadow-lg w-400px">
      <div class="login-header text-center mb-8">
        <h1 class="text-2xl font-bold text-gray-800">考试管理系统</h1>
        <p class="text-gray-600 mt-2">管理员登录</p>
      </div>

      <!-- 登录方式切换 -->
      <div class="login-tabs mb-6">
        <el-tabs v-model="loginType" class="login-type-tabs">
          <el-tab-pane label="用户名登录" name="username" />
          <el-tab-pane label="手机号登录" name="phone" />
        </el-tabs>
      </div>

      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item
          v-if="loginType === 'username'"
          label="用户名"
          prop="username"
        >
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            size="large"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item v-if="loginType === 'phone'" label="手机号" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            size="large"
            autocomplete="off"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            autocomplete="off"
            @keyup.enter="submitForm"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="w-full"
            :loading="loading"
            @click="submitForm"
          >
            {{ loading ? "登录中..." : "登录" }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer text-center mt-6">
        <p class="text-gray-500 text-sm">
          © 2024 考试管理系统. All rights reserved.
        </p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-container {
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-form :deep(.el-form-item__label) {
  color: #606266;
  font-weight: 500;
}

.login-form :deep(.el-input__inner) {
  border-radius: 6px;
}

.login-form :deep(.el-button) {
  border-radius: 6px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.login-type-tabs :deep(.el-tabs__header) {
  margin: 0;
}

.login-type-tabs :deep(.el-tabs__nav-wrap) {
  display: flex;
  justify-content: center;
}

.login-type-tabs :deep(.el-tabs__item) {
  font-size: 16px;
  font-weight: 500;
  color: #909399;
}

.login-type-tabs :deep(.el-tabs__item.is-active) {
  color: #409eff;
}
</style>
